package com.aibaixun.common.utils;

import java.util.Random;

public class IdCardUtils {

    private static final int[] WEIGHTS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    private static final char[] CHECK_DIGITS = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

    public static String generateID() {
        // 生成前 17 位的随机数字和字符
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 17; i++) {
            sb.append(random.nextInt(10));
        }

        // 计算校验位
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (sb.charAt(i) - '0') * WEIGHTS[i];
        }
        int index = sum % 11;

        // 获取校验位字符
        char checkDigit = CHECK_DIGITS[index];

        // 拼接身份证号
        sb.append(checkDigit);

        return sb.toString();
    }

}
